import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo

public class Defender extends Actor
{
    private boolean spaceStillDown = false;
    private SpaceWorld world;
    private int lastY;
    private Counter score;
    private Level level;
    
    public Defender(SpaceWorld world, Counter counter, Level level)
    {
        this.world=world;
        this.level=level;
        score = counter;
    }
    
    public void act() 
    {
        horzMove();
        Fire();
        
        if(score.getValue()<100) level.setLevel(1);
        else if(score.getValue()<200) level.setLevel(2);
        else if(score.getValue()<400) level.setLevel(3);
    }
    
    public void horzMove()
    {
        if (Greenfoot.isKeyDown("A") || Greenfoot.isKeyDown("left")) setLocation(getX()-5,getY());
        else if (Greenfoot.isKeyDown("D") || Greenfoot.isKeyDown("right")) setLocation(getX()+5,getY());
        
        MouseInfo mouseInfo = Greenfoot.getMouseInfo();
        if(mouseInfo!=null)
        {
            int mY = mouseInfo.getX();
            if(lastY!=mY) setLocation(mY,getY());
            lastY = mY;
        }
    }
    
    public void Fire()
    {
        if((Greenfoot.mouseClicked(world) || (level.getLevel()==3 && Greenfoot.isKeyDown("enter"))) && spaceStillDown == false) 
        {
            getWorld().addObject(new Bullet(score), getX(), getY()+200);
            spaceStillDown = true;
            Greenfoot.playSound("sounds/blaster.wav");
        }
        else if (Greenfoot.mouseClicked(world) == false) {
            spaceStillDown = false;
        }
    }
}
